#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Roxane Levy

from som4 import Som
from numpy import genfromtxt,array,linalg,zeros,mean,std,apply_along_axis


data = genfromtxt('iris.csv', delimiter=',',usecols=(0,1,2,3))
data = apply_along_axis(lambda x: x/linalg.norm(x),1,data) # data normalization

som = Som(10,10,4)

print("Début apprentissage...")
som.input_random(data,130)
print("Terminé!")


from pylab import plot,axis,show,pcolor,colorbar,bone
bone()
pcolor(som.distance_map().T) # plotting the distance map as background  , .T is for transposing the matrix
colorbar()
target = genfromtxt('iris.csv',delimiter=',',usecols=(4),dtype=str) #  chargement des labels
t = zeros(len(target),dtype=int)
t[target == 'Iris-setosa'] = 0
t[target == 'Iris-versicolor'] = 1
t[target == 'Iris-virginica'] = 2

markers = ['o','s','D']
colors = ['r','g','b']
for idx,vec in enumerate(data):   #idx est l'index du input vector, vec est le vecteur d'apprentissage en cours
 	bmu = som.findbestmatchingnode(vec)
 	plot(bmu[0]+.5,bmu[1]+.5,markers[t[idx]],markerfacecolor='None',markeredgecolor=colors[t[idx]],markersize=12,markeredgewidth=2)

axis([0,som.weights.shape[0],0,som.weights.shape[1]])
show()
